package com.sjd.storehouse.service;

import com.sjd.storehouse.entity.AssistList;
import com.sjd.storehouse.entity.Center;
import com.sjd.storehouse.entity.Rent;
import com.sjd.storehouse.mapper.StoreMapper;
import com.sjd.storehouse.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StoreService {

    @Resource
    private StoreMapper storeMapper;
    @Resource
    private UserMapper userMapper;

    public int addCenter(Center center) {
        center.setCreateTime(new Date());
        return storeMapper.addCenter(center);
    }

    public List<Center> loadCenter(String query, int curr, int limit) {
        int startIndex = curr * limit - limit;
        if (query == null) {
            query = "";
        }
        return storeMapper.loadCenter(query, startIndex, limit);
    }

    public int countCenter(String query) {
        if (query == null) {
            query = "";
        }
        return storeMapper.countCenter(query);
    }

    public Center getCenter(String id) {
        return storeMapper.getCenterById(id);
    }

    public Center getAssist(String id) {
        return storeMapper.getAssistById(id);
    }

    public int updateCenter(Center center) {
        return storeMapper.updateCenterById(center);
    }

    public int outCenter(String id) {
        Center center = new Center();
        center.setId(id);
        center.setDelFlag("1");
        return storeMapper.updateCenterById(center);
    }

    public int outAssist(String id, int outNum) {
        Center dbAssist = storeMapper.getAssistById(id);
        if (outNum <= 0 || dbAssist.getNum() < outNum) {
            return 0;
        }
        int remainNum = dbAssist.getNum()-outNum; //剩余数量
        Center assist = new Center();
        assist.setId(id);
        if (remainNum==0){
            assist.setDelFlag("1");
        }else {
            assist.setNum(remainNum);
        }
        return storeMapper.updateAssistById(assist);
    }

    public List<AssistList> loadAssistList() {
        return storeMapper.loadAssistList();
    }

    public List<Center> loadAssist(String kind) {
        if (ObjectUtils.isEmpty(kind)) {
            return null;
        }
        return storeMapper.loadAssist(kind);
    }

    public int addAssist(Center assist) {
        if (assist == null) {
            return 0;
        }
        assist.setDelFlag("2");
        assist.setCreateTime(new Date());
        return storeMapper.addAssist(assist);
    }

    public List<Center> loadApp() {
        List<Center> list = storeMapper.loadApp();
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Center dbCenter = list.get(i);
                String kindDetail = storeMapper.getAssistListById(dbCenter.getKind()).getName();
                dbCenter.setKindDetail(kindDetail);
                list.set(i, dbCenter);
            }
        }
        return list;
    }

    public int updateAssist(Center assist) {
        return storeMapper.updateAssistById(assist);
    }

    public String verifyApp(Center assist) {
        Center dbAssist = storeMapper.getAssistById(assist.getId());
        Center dbCenter = storeMapper.getCenterByName(dbAssist.getName());
        if (dbCenter == null) {
            return "仓库中无此类药品";
        }
        int rowNumA;
        int rowNumC;
        int num = dbCenter.getNum() - dbAssist.getNum();  //减去二级库使用的数量
        if (num < 0) {
            return "库存数量不足";
        } else if (num == 0) {
            dbCenter.setNum(num);
            dbCenter.setDelFlag("1");
            rowNumA = storeMapper.updateAssistById(assist);
            rowNumC = storeMapper.updateCenterById(dbCenter);
        } else {
            dbCenter.setNum(num);
            rowNumA = storeMapper.updateAssistById(assist);
            rowNumC = storeMapper.updateCenterById(dbCenter);
        }

        if (rowNumA == 1 && rowNumC == 1) {
            return "操作成功";
        }
        return "参数错误";
    }

    public String verifyRent(Rent rent) {
        //查出rent详细信息
        Rent dbRent = storeMapper.getRentById(rent.getId());
        //修改assist库
        Center assist = new Center();
        assist.setId(dbRent.getAssistId());  //要修改的id
        assist.setKind(dbRent.getKind());   //修改库为目标库
        assist.setUsername(dbRent.getUsername());   //修改入库人
        int rowNumA = storeMapper.updateAssistById(assist);
        //删除rent库记录
        rent.setDelFlag("1");
        int rowNumR = storeMapper.updateRentById(rent);

        if (rowNumA == 1 && rowNumR == 1) {
            return "操作成功";
        }
        return "参数错误";
    }

    public String rentAssist(Rent rent) {
        Rent dbRent = storeMapper.getRentByAssistId(rent.getAssistId());
        if (dbRent != null && rent.getKind().equals(dbRent.getKind())) {
            return "请勿重复申请";
        } else {
            rent.setId(UUID.randomUUID().toString().replace("-", ""));
            int rowNum = storeMapper.addRent(rent);
            return rowNum == 1 ? "请求借调成功" : "系统错误";
        }
    }

    public List<Rent> loadRent(String kind) {
        List<Rent> list = storeMapper.loadRent(kind);
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Rent dbRent = list.get(i);
                String kindDetail = storeMapper.getAssistListById(dbRent.getKind()).getName();
                String name = storeMapper.getAssistById(dbRent.getAssistId()).getName();  //品名
                dbRent.setKindDetail(kindDetail);
                dbRent.setName(name);
                list.set(i, dbRent);
            }
        }
        return list;
    }

    public int updateRent(Rent rent) {
        return storeMapper.updateRentById(rent);
    }
}
